﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calc
{
    public partial class frmLineItemData : Form
    {
        public frmLineItemData()
        {
            InitializeComponent();
        }
        ProjectDB objProjectDB = new ProjectDB();
        ProjectEntities objProjectEntities = new ProjectEntities();
        Entities objEntities = new Entities();

        private void frmLineItemData_Load(object sender, EventArgs e)
        {
            DataSet dsProject = new DataSet();
            objProjectEntities.ProjectID = ProjectDB.ProjectID;
            dsProject = objProjectDB.GetProjectDetails(objProjectEntities);

            if (dsProject.Tables[0].Rows.Count > 0)
                txtProjectName.Text = dsProject.Tables[0].Rows[0]["ProjectName"].ToString();

            DataSet dsMeasurement = new DataSet();
            dsMeasurement = objProjectDB.LoadMeasurements(objProjectEntities);

            if (dsMeasurement.Tables[0].Rows.Count > 0)
            {
                txtDescription.Text = dsMeasurement.Tables[0].Rows[0]["Description"].ToString();
            }

            DataSet dsMeasurementChild = new DataSet();
            dsMeasurementChild = objProjectDB.GetMeasurementChildsByID(ProjectDB.ID);

            if (dsMeasurementChild.Tables[0].Rows.Count > 0)
            {
                txtItemName.Text = dsMeasurementChild.Tables[0].Rows[0]["ItemName"].ToString();
                lblCategory.Text = dsMeasurementChild.Tables[0].Rows[0]["MaterialName"].ToString();
                lblMeasurement1.Text = dsMeasurementChild.Tables[0].Rows[0]["MeasurementUnits"].ToString();
                lblMeasurement2.Text=dsMeasurementChild.Tables[0].Rows[0]["MeasurementUnits"].ToString();
                lblMeasurement3.Text = dsMeasurementChild.Tables[0].Rows[0]["MeasurementUnits"].ToString();
                lblCostPer.Text = lblCostPer.Text + " " + dsMeasurementChild.Tables[0].Rows[0]["MeasurementUnits"].ToString();
                lblRequired.Text = lblRequired.Text + " " + dsMeasurementChild.Tables[0].Rows[0]["MeasurementUnits"].ToString();
                lblLaborCostPer.Text = lblLaborCostPer.Text + " " + dsMeasurementChild.Tables[0].Rows[0]["MeasurementUnits"].ToString();
                txtOriginalMeasurement.Text = dsMeasurementChild.Tables[0].Rows[0]["OriginalMeasurement"].ToString();
                txtCorrectedMeasurement.Text = dsMeasurementChild.Tables[0].Rows[0]["CorrectedMeasurement"].ToString();
                txtLayers.Text = dsMeasurementChild.Tables[0].Rows[0]["Layers"].ToString();
                txtCoverage.Text = dsMeasurementChild.Tables[0].Rows[0]["Coverage"].ToString();
                txtWaste.Text = dsMeasurementChild.Tables[0].Rows[0]["Waste"].ToString();
                txtCostPer.Text = dsMeasurementChild.Tables[0].Rows[0]["Cost"].ToString();
                txtLabour.Text = txtCorrectedMeasurement.Text;
                txtLabourCostPer.Text = dsMeasurementChild.Tables[0].Rows[0]["Labour"].ToString();

                Calculate();

            }
        }
        private void Calculate()
        {
            decimal waste = 0;
            decimal avg = 0;
            decimal PurchaseUnits = 0;
            decimal Material = 0;
            decimal Labour = 0;
            decimal Total = 0;
            decimal CorrectedMeasurement=0;
            decimal Layers=0;
            decimal Coverge=0;
            decimal CostPurchasePerUnit=0;
            if (txtCorrectedMeasurement.Text != "")
                CorrectedMeasurement = Convert.ToDecimal(txtCorrectedMeasurement.Text);
            else
                CorrectedMeasurement = 0;
            if (txtLayers.Text != "")
            {
                Layers = Convert.ToDecimal(txtLayers.Text);
                Layers = decimal.Round(Layers, 3);
            }
            else
                Layers = 0;
            if (txtCoverage.Text != "")
            {
                Coverge = Convert.ToDecimal(txtCoverage.Text);
                Coverge = decimal.Round(Coverge, 3);
            }
            else
                Coverge = 1;
            if (txtCostPer.Text != "")
            {
                CostPurchasePerUnit = Convert.ToDecimal(txtCostPer.Text);
                CostPurchasePerUnit = decimal.Round(CostPurchasePerUnit, 3);
            }
            else
                CostPurchasePerUnit = 0;



            decimal res = (CorrectedMeasurement * Layers) / Coverge;

            if (txtWaste.Text != "")
            {
                waste = Convert.ToDecimal(txtWaste.Text);
                waste = waste / 100;

                avg = res * waste;
            }
            PurchaseUnits = res + avg;
            txtRequired.Text = Convert.ToString(decimal.Round(PurchaseUnits,3));
            Material = PurchaseUnits * CostPurchasePerUnit;
            txtMaterialCost.Text = Convert.ToString(decimal.Round(Material, 3));

            if (txtLabourCostPer.Text != "")
                Labour = CorrectedMeasurement * Convert.ToDecimal(txtLabourCostPer.Text);
            else
                Labour = 0;
            
            txtLabourCost.Text = Convert.ToString(decimal.Round(Labour,3));

            Total=Convert.ToDecimal(txtMaterialCost.Text) + Convert.ToDecimal(txtLabourCost.Text);
            txtTotal.Text = Convert.ToString(decimal.Round(Total, 3));
        }

        private void txtChanged(object sender, EventArgs e)
        {
            Calculate();
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            
            objProjectEntities.Action = 1;
            objProjectEntities.ID = ProjectDB.ID;
            objEntities.ItemName = txtItemName.Text;
            objProjectEntities.Measurement = 0;
            objProjectEntities.MeasurementUnits = "";
            objProjectEntities.OriginalMeasurement = Convert.ToInt16(txtOriginalMeasurement.Text);
            objProjectEntities.CorrectedMeasurement = Convert.ToInt16(txtCorrectedMeasurement.Text);
            objProjectEntities.Layers = Convert.ToInt16(txtLayers.Text);
            objEntities.Coverage = txtCoverage.Text;
            objEntities.Waste = txtWaste.Text;
            objEntities.Cost = Convert.ToDecimal(txtCostPer.Text);
            objEntities.Labour = txtLabourCostPer.Text;

            objProjectDB.ManageMeasurementChild(objProjectEntities.ID, objProjectEntities.Measurement, objProjectEntities.OriginalMeasurement, objProjectEntities.CorrectedMeasurement, objProjectEntities.Notes, objProjectEntities.MeasurementUnits, objProjectEntities.Layers, objEntities.ItemID, objEntities.ItemName, objEntities.GroupID, objEntities.MaterialTypeID, objEntities.Cost, objEntities.CostPer, objEntities.Coverage, objEntities.Waste, objEntities.Labour, objEntities.LabourType, objProjectEntities.Action);
            this.Dispose();
            frmMainTab obj = new frmMainTab();
            obj.BindDatatoWorkSheet();
        }
        
    }
}
